package com.nervenets.shopping.controller.shopping.params;

import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.shopping.hibernate.domain.shopping.Product;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.springframework.data.domain.Sort;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class ProductPagingParams extends EntityPagingParams<Product> {
    @ApiModelProperty("是否启用：0所有，1启用，2禁用")
    private int enable;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addYesOrNoPredicate(root.get("enable"), enable, 1, 2);
    }

    public List<Sort.Order> orders() {
        return new ArrayList<Sort.Order>() {{
            add(new Sort.Order(Sort.Direction.ASC, "queue"));
        }};
    }
}
